version 17.0
clear all
cd "MYPATH\derived\build_model_sample"
adopath + ../../ado/
pause on

cap log close

log using "analysis.log", replace
preliminaries
foreach PATH in RESULTS TEMP {
	cap mkdir "${`PATH'}\derived"
	cap mkdir "${`PATH'}\derived\build_model_sample"
    if "`PATH'" != "TEMP" cap mkdir "${`PATH'}\derived\build_model_sample\figures"
}

graph set window fontface default
graph set ps fontface default
graph set window fontfacemono "Consolas"
graph set ps fontfacemono "Consolas"


program main
	xs_stats_compact
	*xs_stats
	*rm $TEMP\derived\build_model_sample\all_live_births.dta
	*rm $TEMP\derived\build_model_sample\all_live_universalnt.dta
end

program xs_stats_compact
	forval samp = 1/4 {
	    if `samp' == 1 use $TEMP\derived\build_model_sample\all_live_births.dta, clear
		if `samp' == 2 use $TEMP\derived\build_model_sample\all_live_universalnt.dta, clear
		if `samp' == 3 use $DATA\all_screens_universalnt_w_xs.dta, clear
		if `samp' == 4 use $DATA\model_sample.dta, clear
		qui count 
		local n_pregs = r(N)
		qui unique lopnr 
		local n_moms = r(unique)
		* N
		*matrix x_means = nullmat(x_means) \ (`n_pregs')
		
		* Demographics
		foreach var in married mom_foreign {
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			matrix x_means = nullmat(x_means) \ (`share_pregs')
		}
		qui sum age 
		local share_pregs = r(mean)
		matrix x_means = nullmat(x_means) \ (`share_pregs')
		qui gen age1 = 1 * (age < 25)
		qui gen age2 = 1 * (age >= 25 & age < 35)
		qui gen age3 = 1 * (age >= 35 & !mi(age))
		forval i = 1/3 {
			qui sum age`i'
			local share_pregs = r(mean)
			matrix x_means = nullmat(x_means) \ (`share_pregs')
		}
		
		local dollar_per_sek = 567.5 / 5000
		replace hh_inc_smooth = hh_inc_smooth * `dollar_per_sek'
		qui sum hh_inc_smooth 
		local share_pregs = r(mean)
		matrix x_means = nullmat(x_means) \ (`share_pregs')
		forval i = 1/4 {
			qui gen inc_quartile`i' = 1 * (inc_quartile == `i')
			qui sum inc_quartile`i'
			local share_pregs = r(mean)
			matrix x_means = nullmat(x_means) \ (`share_pregs')
		}
		qui gen inc_quartile_mis = 1 * (mi(inc_quartile))
		qui sum inc_quartile_mis
		local share_pregs = r(mean)
		matrix x_means = nullmat(x_means) \ (`share_pregs')
		forval i = 1/3 {
			qui gen educ`i' = 1 * (educ == `i')
			qui sum educ`i'
			local share_pregs = r(mean)
			matrix x_means = nullmat(x_means) \ (`share_pregs')
		}
		
		qui gen educ4 = 1*mi(educ)
		qui sum educ4
		local share_pregs = r(mean)
		qui unique lopnr if educ4 == 1
		matrix x_means = nullmat(x_means) \ (`share_pregs')
		
		foreach var in dv_prev_kids {
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			matrix x_means = nullmat(x_means) \ (`share_pregs')
		}
		
		* Info from previous births
		*foreach var in prev_mis_still prev_birth_issue prev_concern prev_any_q_icd {
		foreach var in prev_birth_issue prev_concern prev_any_q_icd {
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			matrix x_means = nullmat(x_means) \ (`share_pregs')
		}
		* N
		matrix x_means_`samp' =  (`n_pregs') \ x_means
		matrix drop x_means 

	}
	
	matrix x_means = x_means_1, x_means_2, x_means_3, x_means_4
	matrix list x_means 
	
	frmttable using "${RESULTS}\derived\build_model_sample\x_means_compact.tex", statmat(x_means) ///
	  rtitles("N" \ "Married" \ "Foreign-born"\ "Maternal age" \ "< 25" \ "25-34" \ "35+" \ ///  
		"Household income" \ "Income: q1" \ "Income: q2" \ "Income: q3" \ "Income: q4" \ "Income: missing" \ ///
		"No college"\ "Some college" \ "College" \ "Missing educ" \ ///
		"Any previous kids" \ ///
		"Previous birth issue" \ "Previous birth of concern" \ ///
		"Previous congen deform or CA ")  ///
	  ctitles("", "All live births", "All live births - Universal NT",  ///
	    "NT screens - Univeral NT", "Baseline sample",) ///
	  tex sdec(3 \ 3 \ 3 \ 3 \ 3 \ 3 \ 3 \ 3 \ 3 \ 3 \3 \ 3\ 3\ 3 \ 3 \ 3 \3 \3 \ 3 \ 3\ 3 \ 0 ) replace
		
	putexcel set "${RESULTS}\derived\build_model_sample\x_means_compact", replace
	putexcel A1 = matrix(x_means), nformat(number_d2)
end


program xs_stats
	matrix drop _all
	forval samp = 1/4 {
	    if `samp' == 1 use $TEMP\derived\build_model_sample\all_live_births.dta, clear
		if `samp' == 2 use $TEMP\derived\build_model_sample\all_live_universalnt.dta, clear
		if `samp' == 3 use $DATA\all_screens_universalnt_w_xs.dta, clear
		if `samp' == 4 use $DATA\model_sample.dta, clear
		qui count 
		local n_pregs = r(N)
		qui unique lopnr 
		local n_moms = r(unique)
		* Demographics
		qui gen age1 = 1 * (age < 20)
		qui gen age2 = 1 * (age >= 20 & age < 30)
		qui gen age3 = 1 * (age >= 30 & age < 40)
		qui gen age4 = 1 * (age >= 40 & !mi(age))
		forval i = 1/4 {
			qui sum age`i'
			local share_pregs = r(mean)
			qui unique lopnr if age`i' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		forval i = 1/3 {
			qui gen educ`i' = 1 * (educ == `i')
			qui sum educ`i'
			local share_pregs = r(mean)
			qui unique lopnr if educ`i' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		
		qui gen educ4 = 1*mi(educ)
		qui sum educ4
		local share_pregs = r(mean)
		qui unique lopnr if educ4 == 1
		local share_moms = r(unique) / `n_moms'
		matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		forval i = 1/4 {
			qui gen inc_quartile`i' = 1 * (inc_quartile == `i')
			qui sum inc_quartile`i'
			local share_pregs = r(mean)
			qui unique lopnr if inc_quartile`i' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		foreach var in married mom_foreign {
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		* Previous kids
		forval i = 1/4 {
			gen kids`i' = 1 * num_prev_kids == `i'
		}
		replace kids4 = 1 * (num_prev_kids >= 4)
		foreach var in dv_prev_kids kids1 kids2 kids3 kids4 {
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		
		* Info from previous births
		foreach var in dv_prev_mis dv_prev_mis_hdia prev_stillbirth prev_death_28 prev_preterm_live prev_chrom_ab ///
		  prev_congen_deform prev_pnatal_issue {
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		foreach num of numlist 1/12 {
			local var "prev_pnatal_issue`num'"
			qui sum `var'
			local share_pregs = r(mean)
			qui unique lopnr if `var' == 1
			local share_moms = r(unique) / `n_moms'
			matrix x_means = nullmat(x_means) \ (`share_pregs', `share_moms')
		}
		
		sum *pnatal*

		matrix list x_means
		matrix x_means_`samp' = x_means \ (`n_pregs', `n_moms')
		matrix drop x_means 
	}
	
	matrix x_means = x_means_1, x_means_2, x_means_3, x_means_4
	matrix list x_means 
	
	frmttable using "${RESULTS}\derived\build_model_sample\x_means.tex", statmat(x_means) ///
	  rtitles("< 20" \ "20-29" \ "30-39" \ "40+" \ ///  
		"No college"\ "Some college" \ "College" \ "Missing educ" \ ///
	    "Income: q1" \ "Income: q2" \ "Income: q3" \ "Income: q4" \ ///
		"Married" \ "Foreign-born" \ ///
		"Any previous kids" \ "1 prev live birth" \ "2 prev live birth" \ "3 prev live birth" \ "4+ prev live birth" \ ///
		"Miscarriage" \ "Mis (hdia)" \ "Stillbirth" \ "Death within 28 days" \ "Preterm live birth" \ "CA" \ ///
		"Congenital deformation"  \ "Any previous perinatal ICD code" \ ///
		"P00-P04" \ "P05-P08" \ "P09" \ "P10-P15" \ "P19-P29" \ "P35-P39" \ "P50-P61" \ "P70-P74" \ ///
		"P76-P78" \ "P80-P83" \ "P84" \ "P90-P96" \ "N")  ///
	  ctitles("", "Share of pregnancies", "Share of mothers","Share of pregnancies", "Share of mothers", "Share of pregnancies", "Share of mothers", "Share of pregnancies", "Share of mothers") ///
	  tex sdec(3) replace
		
	putexcel set "${RESULTS}\derived\build_model_sample\x_means", replace
	putexcel A1 = matrix(x_means), nformat(number_d2)
end


* EXECUTE 
main
